home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / joysti1a / form1.frm (.txt) next >
Encoding:
Visual Basic Form  |  1998-02-17  |  6.6 KB  |  199 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "joystick data"
  5.    ClientHeight    =   855
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   2820
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   855
  13.    ScaleWidth      =   2820
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.CheckBox button 
  16.       Caption         =   "button 1"
  17.       Height          =   255
  18.       Index           =   0
  19.       Left            =   1800
  20.       TabIndex        =   2
  21.       Top             =   240
  22.       Width           =   1095
  23.    End
  24.    Begin VB.Timer Timer1 
  25.       Enabled         =   0   'False
  26.       Interval        =   100
  27.       Left            =   2400
  28.       Top             =   600
  29.    End
  30.    Begin VB.Label status 
  31.       Caption         =   "status"
  32.       Height          =   255
  33.       Left            =   0
  34.       TabIndex        =   3
  35.       Top             =   600
  36.       Width           =   2415
  37.    End
  38.    Begin VB.Label label 
  39.       Caption         =   "X"
  40.       Height          =   255
  41.       Index           =   0
  42.       Left            =   120
  43.       TabIndex        =   1
  44.       Top             =   240
  45.       Width           =   375
  46.    End
  47.    Begin VB.Label axis 
  48.       BorderStyle     =   1  'Fixed Single
  49.       Caption         =   " "
  50.       Height          =   255
  51.       Index           =   0
  52.       Left            =   600
  53.       TabIndex        =   0
  54.       Top             =   240
  55.       Width           =   975
  56.    End
  57. Attribute VB_Name = "Form1"
  58. Attribute VB_GlobalNameSpace = False
  59. Attribute VB_Creatable = False
  60. Attribute VB_PredeclaredId = True
  61. Attribute VB_Exposed = False
  62. Option Explicit
  63. Dim ji As JOYINFOEX     ' joystick state buffer
  64. Dim caps As JOYCAPS     ' joystick capabilities
  65. Dim rc As Long          ' return code
  66. Dim i As Long           ' index
  67. Dim mask As Long        ' bitmask
  68. Dim xaxis As label      ' x-axis control
  69. Dim yaxis As label      ' y-axis control
  70. Dim zaxis As label      ' z-axis control
  71. Dim raxis As label      ' r-axis control
  72. Dim uaxis As label      ' u-axis control
  73. Dim vaxis As label      ' v-axis control
  74. Dim pov As label        ' pov-axis control
  75. Dim numAxes As Long     ' number of axes added to form
  76. Dim axisY As Long       ' Y value for current axis control being added
  77. Const ySpacingFactor = 1.4  ' spacing between controls
  78. Private Sub Form_Load()
  79.    ' Start with timer disabled
  80.    Timer1.Enabled = False
  81.    ' Get capabilities of joystick1
  82.    rc = joyGetDevCaps(JOYSTICKID1, caps, Len(caps))
  83.    If (rc <> 0) Then
  84.        MsgBox "Couldn't detect the joystick"
  85.        End
  86.    End If
  87.    ' Add axis controls
  88.    numAxes = 1
  89.    axisY = axis(0).Top
  90.    Set xaxis = axis(0)
  91.    AddAxisControl "Y", yaxis
  92.    If (caps.wCaps And JOYCAPS_HASZ) Then AddAxisControl "Z", zaxis
  93.    If (caps.wCaps And JOYCAPS_HASR) Then AddAxisControl "R", raxis
  94.    If (caps.wCaps And JOYCAPS_HASU) Then AddAxisControl "U", uaxis
  95.    If (caps.wCaps And JOYCAPS_HASV) Then AddAxisControl "V", vaxis
  96.    If (caps.wCaps And JOYCAPS_HASPOV) Then AddAxisControl "POV", pov
  97.    ' Create checkboxes for the buttons
  98.    LoadButtonCheckBoxes (caps.wNumButtons)
  99.    ' Position the status box
  100.    status.Left = 0
  101.    status.Top = Form1.Height - (status.Height * 2.5)
  102.    ' Start the timer
  103.    Timer1.Enabled = True
  104. End Sub
  105. 'Add label controls for a supported axis
  106. Private Sub AddAxisControl(name As String, ctrl As label)
  107.    axisY = axisY + (axis(0).Height * ySpacingFactor)
  108.    If (Form1.Height < (axisY + axis(0).Height * 3)) Then
  109.       Form1.Height = Form1.Height + (axis(0).Height * ySpacingFactor)
  110.    End If
  111.    Load axis(numAxes)
  112.    Load label(numAxes)
  113.    Set ctrl = axis(numAxes)
  114.    ctrl.Width = axis(0).Width
  115.    ctrl.Height = axis(0).Height
  116.    ctrl.Left = axis(0).Left
  117.    ctrl.Top = axisY
  118.    ctrl.Visible = True
  119.    label(numAxes).Width = label(0).Width
  120.    label(numAxes).Height = label(0).Height
  121.    label(numAxes).Left = label(0).Left
  122.    label(numAxes).Top = axisY
  123.    label(numAxes).Visible = True
  124.    label(numAxes).Caption = name
  125.    numAxes = numAxes + 1
  126. End Sub
  127. ' Add a checkbox for each of the buttons on the joystick
  128. Private Sub LoadButtonCheckBoxes(numButtons As Long)
  129.    If (numButtons = 0) Then
  130.       button(0).Visible = False
  131.       Form1.Width = button(0).Left
  132.    Else
  133.       If (numButtons > 1) Then
  134.          Dim curX As Long
  135.          Dim curY As Long
  136.          Dim i As Long
  137.       
  138.          curX = button(0).Left
  139.          curY = button(0).Top
  140.          
  141.          For i = 1 To (numButtons - 1)
  142.             ' move down for the next control
  143.             curY = curY + (button(0).Height * ySpacingFactor)
  144.             
  145.             'start new column if necessary
  146.             If (Form1.Height < (curY + 3 * button(0).Height)) Then
  147.                 curY = button(0).Top
  148.                 curX = curX + button(0).Width
  149.             End If
  150.             
  151.             'expand form if necessary
  152.             If (Form1.Width < (curX + button(0).Width)) Then
  153.                 Form1.Width = curX + button(0).Width
  154.             End If
  155.             
  156.             ' load checkbox and set properties
  157.             Load button(i)
  158.             button(i).Top = curY
  159.             button(i).Left = curX
  160.             button(i).Width = button(0).Width
  161.             button(i).Height = button(0).Height
  162.             button(i).Visible = True
  163.             button(i).Caption = "button " & i + 1
  164.          Next
  165.       End If
  166.    End If
  167. End Sub
  168. ' Poll the joystick
  169. Private Sub Timer1_Timer()
  170.    ' Initialize struct
  171.    ji.dwSize = Len(ji)
  172.    ji.dwFlags = JOY_RETURNALL
  173.    ' Get the current joystick data
  174.    rc = joyGetPosEx(JOYSTICKID1, ji)
  175.    ' Display the status
  176.    If (rc = 0) Then
  177.       status.Caption = "status: joystick connected"
  178.    Else
  179.       If (rc = JOYERR_UNPLUGGED) Then
  180.          status.Caption = "status: joystick unplugged"
  181.       Else
  182.          status.Caption = "status: joyGetPosEx error, rc = " & rc
  183.       End If
  184.    End If
  185.    ' Display the data on the form
  186.    xaxis.Caption = ji.dwXpos
  187.    yaxis.Caption = ji.dwYpos
  188.    If (caps.wCaps And JOYCAPS_HASZ) Then zaxis.Caption = ji.dwZpos
  189.    If (caps.wCaps And JOYCAPS_HASR) Then raxis.Caption = ji.dwRpos
  190.    If (caps.wCaps And JOYCAPS_HASU) Then uaxis.Caption = ji.dwUpos
  191.    If (caps.wCaps And JOYCAPS_HASV) Then vaxis.Caption = ji.dwVpos
  192.    If (caps.wCaps And JOYCAPS_HASPOV) Then pov.Caption = ji.dwPOV
  193.    mask = 1
  194.    For i = 0 To (caps.wNumButtons - 1)
  195.       If (ji.dwButtons And mask) Then button(i).Value = 1 Else button(i).Value = 0
  196.       mask = mask * 2
  197.    Next
  198. End Sub
  199.